/**
 * 单个订单打印模板工具类
 */
import { hiprint, defaultElementTypeProvider } from 'vue-plugin-hiprint'

class OrderPrintTemplate {
  /**
   * 执行单个订单打印
   * @param {Object} order - 订单数据
   */
  print(order) {
    // 初始化hiprint，注册默认元素类型提供者
    hiprint.init({
      providers: [new defaultElementTypeProvider()]
    })

    // 创建打印模板实例，定义打印模板结构
    const printTemplate = new hiprint.PrintTemplate({
      template: {
        "panels": [
          {
            "index": 0,
            "name": 1,
            "height": 100,
            "width": 220,
            "paperHeader": 13.5,
            "paperFooter": 238,
            "printElements": [
              {
                "options": {
                  "left": 242.5,
                  "top": 27.5,
                  "height": 9.75,
                  "width": 120,
                  "title": "订单信息",
                  "right": 362.49998474121094,
                  "bottom": 37.24999237060547,
                  "vCenter": 302.49998474121094,
                  "hCenter": 32.37499237060547,
                  "coordinateSync": false,
                  "widthHeightSync": false,
                  "qrCodeLevel": 0
                },
                "printElementType": {
                  "title": "文本",
                  "type": "text"
                }
              },
              {
                "options": {
                  "left": 30,
                  "top": 117.5,
                  "height": 57,
                  "width": 550,
                  "field": "table",
                  "columns": [
                    [
                      {
                        "width": 96,
                        "title": "客户名称",
                        "field": "customer_name",
                        "checked": true,
                        "fixed": false,
                        "rowspan": 1,
                        "colspan": 1
                      },
                      {
                        "width": 158,
                        "title": "产品名称",
                        "field": "product_name",
                        "checked": true,
                        "fixed": false,
                        "rowspan": 1,
                        "colspan": 1
                      },
                      {
                        "width": 78,
                        "title": "婴儿名称",
                        "field": "baby_name",
                        "checked": true,
                        "fixed": false,
                        "rowspan": 1,
                        "colspan": 1
                      },
                      {
                        "width": 216,
                        "title": "备注",
                        "field": "notes",
                        "checked": true,
                        "fixed": false,
                        "rowspan": 1,
                        "colspan": 1
                      },
                    ]
                  ]
                },
                "printElementType": {
                  "title": "空白表格",
                  "type": "table",
                }
              },
              {
                "options": {
                  "left": 42.5,
                  "top": 37.5,
                  "height": 40,
                  "width": 40,
                  "title": "二维码",
                  "qrcodeType": "qrcode",
                  "testData": "qrcode",
                  "right": 82,
                  "bottom": 78,
                  "vCenter": 62,
                  "hCenter": 60,
                  "qrCodeLevel": 0,
                  "coordinateSync": false,
                  "widthHeightSync": false,
                  "hideTitle": true
                },
                "printElementType": {
                  "title": "二维码",
                  "type": "qrcode"
                }
              }
            ],
          }
        ]
      }
    })

    // 准备打印数据，将订单数据转换为打印所需格式
    const printData = {
      order_number: order.order_number || '-',
      // 添加表格数据 - 使用正确的字段名和格式
      table: [{
        customer_name: (order.customer && order.customer.customer_name) || order.customer_name || '-',
        product_name: (order.product && order.product.product_name) || order.product_name || '-',
        baby_name: order.baby_name || '-',
        notes: order.notes || '-'
      }]
    }

    // 执行打印预览
    printTemplate.print(printData)
  }
}

export default OrderPrintTemplate
